package com.gs.cd.service.resource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gs.cd.model.resource.SysSale;
import com.gs.cd.service.resource.mapper.SysSaleMapper;
import com.gs.cd.service.resource.service.SysResourceGroupService;
import com.gs.cd.service.resource.service.SysSaleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author seven
 * @since 2020-12-22
 */
@Service
public class SysSaleServiceImpl extends ServiceImpl<SysSaleMapper, SysSale> implements SysSaleService {

    @Override
    public SysSale getByCode(String saleCode) {
        return getOne(new QueryWrapper<SysSale>().lambda().eq(SysSale::getCode, saleCode));
    }

    @Override
    public boolean removeSaleAndRelationsByCode(String saleCode) {
        //删除资源组
        boolean b1 = sysResourceGroupService.removeAndRelationsBySaleCode(saleCode);
        // TODO: 2020/12/23 删除默认角色
        //删除销售单元
        boolean remove = remove(new QueryWrapper<SysSale>().lambda().eq(SysSale::getCode, saleCode));
        return remove;
    }

    @Autowired
    private SysResourceGroupService sysResourceGroupService;

    @Override
    public boolean removeSaleAndRelationsById(String id) {
        SysSale byId = getById(id);
        if (byId == null) return false;
        return removeSaleAndRelationsByCode(byId.getCode());
    }

    @Override
    public List<SysSale> listByProjectId(String projectId) {
        return list(new QueryWrapper<SysSale>().lambda().eq(SysSale::getProjectId, projectId));
    }


}
